<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.buvaka.features.*"%>
<%@page import="java.net.UnknownHostException"%>
<%@page import="com.buvaka.UpdateGeoJsonFile"%>
<%@ page import = "com.mongodb.*" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="org.bson.types.ObjectId" %>
<html>
<head>
    <title>Изменить объект</title>
</head>
<body>



<%
    try{
        MongoClient client = new MongoClient();
        DB db = client.getDB("gis");
        DBCollection categories = db.getCollection("categories");
        DBCollection points = db.getCollection("points");

        request.setAttribute("points", points);
        request.setAttribute("client", client);
        request.setAttribute("categories", categories);

    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

%>
        <form action="EditPoint.jsp" method="GET">
            Выберите объект для редактирования: <select name="object">
            <%
                DBCursor pointsNames = ((DBCollection)request.getAttribute("points")).find();
                while (pointsNames.hasNext()){
            %>
                <option><%=pointsNames.next().get("name") %></option>
            <%}%>
            </select>
            <br/>
            <input type="submit" value="Выбрать" />

        </form>

<%

    //TODO: horoor
    if (request.getParameter("object") != "" && request.getParameter("object") != null ) {
        BasicDBObject doc = new BasicDBObject("name", request.getParameter("object"));
        DBObject obj = ((DBCollection)request.getAttribute("points")).findOne(doc);

        out.println("<form action=\"updatePoint\" method=\"POST\">");

        out.println("<br/>");

        out.println("Имя: <input type=\"text\" name=\"name\" value = \"" + obj.get("name") + "\" /><br/>");
        out.println("Широта: <input type=\"text\" name=\"lat\" value = \"" + ((BasicDBList )((DBObject)obj.get("geometry")).get("coordinates")).get(1).toString() + "\" /> <br/>");
        out.println("Долгота: <input type=\"text\" name=\"lng\" value = \"" + ((BasicDBList )((DBObject)obj.get("geometry")).get("coordinates")).get(0).toString() + "\" /><br/>");
        out.println("<textarea rows=\"10\" cols=\"45\" name=\"description\">" + obj.get("description") +"</textarea><br/>");

        DBCursor categoriesNames = ((DBCollection)request.getAttribute("categories")).find();

        String cat = (String)obj.get("category");
        out.println ("Тип: <select name=\"category\">");
        while (categoriesNames.hasNext()){

            String catName = (String)categoriesNames.next().get("_id");
            String selected = catName.equals(cat) ? "selected" : "";
            out.println ("<option " + selected + ">" + catName + "</option>");
        }
        out.println ("</select>");

        out.println("<input type=\"submit\" value=\"Сохранить\" />");

        out.println("<form>");
    }
%>



</body>
</html>
